package com.listener;

import com.alibaba.fastjson.JSON;
import com.mapper.BaseMapper;
import com.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by head79 on 2017/6/22.
 */
public class getCahe implements ServletContextListener {

    private static Logger logger= LoggerFactory.getLogger(getCahe.class);

    public List<LinkedHashMap<String, Object>> cacheUser(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        BaseMapper baseMapper = (BaseMapper) context.getBean("baseMapper");
        RedisUtil redisUtil = (RedisUtil) context.getBean("redisUtil");
        String sql = "select * from user where 1=1";
        List<LinkedHashMap<String, Object>> linkedHashMap =  baseMapper.superSelect(sql);
        logger.info("已经取出数据缓存！");
        logger.info("如下："+JSON.toJSONString(linkedHashMap));
        return linkedHashMap;
    }

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        this.cacheUser();
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        logger.info("listener 被销毁！");
    }
}
